import urllib
import datetime
import csv
import os
from   config import DATADIR

def downloadQuoteData( symbol, start_date, end_date ):
    ''' download data from yahoo '''
    
    start_year, start_month, start_day  = start_date.split( '-' )
    start_month                         = str( int( start_month ) - 1 )
    end_year, end_month, end_day        = end_date.split( '-' )
    
    url_string = "http://ichart.finance.yahoo.com/table.csv?s={0}".format( symbol )
    
    url_string += "&a={0}&b={1}&c={2}".format( start_month, start_day, start_year )
    url_string += "&d={0}&e={1}&f={2}".format( end_month, end_day, end_year )
    
    datafile = urllib.urlopen(url_string).readlines()    
    datafile.reverse()
    head = datafile[-1][:-1].split(',')
    datafile = datafile[ :-1 ]
    
    data = []
    for l in datafile:
        rec = l[:-1].split(',')  #each line ends with \n, so we strip that off before splitting the data
        data.append( rec )
        
    return [ head ] + data 
    
def updateQuoteDataFile( symbol, end_date = None ):
    ''' update the quote data file on disk with newdata append to 
        the end of existing files '''
        
    if not end_date:
        end_date = datetime.date.today().isoformat()
    
    try:
        f = open('%s/%s.csv'%(DATADIR, symbol), 'r')
    except:
        print 'file does not exist for %s' %symbol
        return False
        
    rec = f.readlines()
    f.close()
    if len(rec) > 0:
        last_date = rec[-1].split(',')[0]
    else:
        last_date = '2000-1-1' # if file is empty, start from 2000-1-1
        
    today = datetime.date.today()
    yy, mm, dd = last_date.split('-')
    start_date = datetime.date(int(yy), int(mm), int(dd)) + datetime.timedelta(1)
    
    if today <= start_date:
        print 'data for %s is already most updated' %symbol
        return False
    else:
        start_date = start_date.isoformat()
        new_data = downloadQuoteData( symbol, start_date, end_date )
    
        if len(new_data) > 1:
            f = open('%s/%s.csv'%(DATADIR, symbol), 'a')
            w = csv.writer( f )
            w.writerows( new_data[1:] )
            f.close()
            return True
        else:
            print 'no new data from web for %s' %symbol
            return False
            


            
            
        